home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / demos / xdemo.c < prev   
Encoding:
C/C++ Source or Header  |  1997-01-29  |  7.3 KB  |  329 lines

  1. /* xdemo.c */
  2.  
  3.  
  4. /*
  5.  * Very simple demo of how to use the Mesa/X11 interface instead of the
  6.  * glx, tk or aux toolkits.  I highly recommend using the GLX interface
  7.  * instead of the X/Mesa interface, however.
  8.  *
  9.  * This program is in the public domain.
  10.  *
  11.  * Brian Paul
  12.  */
  13.  
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/Xutil.h>
  21. #include "GL/xmesa.h"
  22. #include "GL/gl.h"
  23.  
  24.  
  25.  
  26. static GLint Black, Red, Green, Blue;
  27.  
  28.  
  29.  
  30. static void make_window( char *title, int color_flag )
  31. {
  32.    int x = 10, y = 10, width = 400, height = 300;
  33.    Display *dpy;
  34.    int scr;
  35.    Window root, win;
  36.    Colormap cmap;
  37.    XColor xcolor;
  38.    int attr_flags;
  39.    XVisualInfo *visinfo;
  40.    XSetWindowAttributes attr;
  41.    XTextProperty tp;
  42.    XSizeHints sh;
  43.    XEvent e;
  44.    XMesaContext context;
  45.    XMesaVisual visual;
  46.    XMesaBuffer buffer;
  47.  
  48.  
  49.    /*
  50.     * Do the usual X things to make a window.
  51.     */
  52.  
  53.    dpy = XOpenDisplay(NULL);
  54.    if (!dpy) {
  55.       printf("Couldn't open default display!\n");
  56.       exit(1);
  57.    }
  58.  
  59.    scr = DefaultScreen(dpy);
  60.    root = RootWindow(dpy, scr);
  61.  
  62.    /* alloc visinfo struct */
  63.    visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
  64.  
  65.    /* Get a visual and colormap */
  66.    if (color_flag) {
  67.       /* Open TrueColor window */
  68.  
  69. /*
  70.       if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
  71.      printf("Couldn't get 24-bit TrueColor visual!\n");
  72.      exit(1);
  73.       }
  74. */
  75.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  76.      printf("Couldn't get 8-bit PseudoColor visual!\n");
  77.      exit(1);
  78.       }
  79.  
  80.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  81.       Black = Red = Green = Blue = 0;
  82.    }
  83.    else {
  84.       /* Open color index window */
  85.  
  86.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  87.      printf("Couldn't get 8-bit PseudoColor visual\n");
  88.      exit(1);
  89.       }
  90.  
  91.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  92.  
  93.       /* Allocate colors */
  94.       xcolor.red   = 0x0;
  95.       xcolor.green = 0x0;
  96.       xcolor.blue  = 0x0;
  97.       xcolor.flags = DoRed | DoGreen | DoBlue;
  98.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  99.      printf("Couldn't allocate black!\n");
  100.      exit(1);
  101.       }
  102.       Black = xcolor.pixel;
  103.  
  104.       xcolor.red   = 0xffff;
  105.       xcolor.green = 0x0;
  106.       xcolor.blue  = 0x0;
  107.       xcolor.flags = DoRed | DoGreen | DoBlue;
  108.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  109.      printf("Couldn't allocate red!\n");
  110.      exit(1);
  111.       }
  112.       Red = xcolor.pixel;
  113.  
  114.       xcolor.red   = 0x0;
  115.       xcolor.green = 0xffff;
  116.       xcolor.blue  = 0x0;
  117.       xcolor.flags = DoRed | DoGreen | DoBlue;
  118.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  119.      printf("Couldn't allocate green!\n");
  120.      exit(1);
  121.       }
  122.       Green = xcolor.pixel;
  123.  
  124.       xcolor.red   = 0x0;
  125.       xcolor.green = 0x0;
  126.       xcolor.blue  = 0xffff;
  127.       xcolor.flags = DoRed | DoGreen | DoBlue;
  128.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  129.      printf("Couldn't allocate blue!\n");
  130.      exit(1);
  131.       }
  132.       Blue = xcolor.pixel;
  133.    }
  134.  
  135.    /* set window attributes */
  136.    attr.colormap = cmap;
  137.    attr.event_mask = ExposureMask | StructureNotifyMask;
  138.    attr.border_pixel = BlackPixel( dpy, scr );
  139.    attr.background_pixel = BlackPixel( dpy, scr );
  140.    attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
  141.  
  142.    /* Create the window */
  143.    win = XCreateWindow( dpy, root, x,y, width, height, 0,
  144.                 visinfo->depth, InputOutput,
  145.                 visinfo->visual,
  146.                 attr_flags, &attr);
  147.    if (!win) {
  148.       printf("Couldn't open window!\n");
  149.       exit(1);
  150.    }
  151.  
  152.    XStringListToTextProperty(&title, 1, &tp);
  153.    sh.flags = USPosition | USSize;
  154.    XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
  155.    XMapWindow(dpy, win);
  156.    while (1) {
  157.       XNextEvent( dpy, &e );
  158.       if (e.type == MapNotify && e.xmap.window == win) {
  159.      break;
  160.       }
  161.    }
  162.  
  163.  
  164.    /*
  165.     * Now do the special Mesa/Xlib stuff!
  166.     */
  167.  
  168.    visual = XMesaCreateVisual( dpy, visinfo,
  169.                               (GLboolean) color_flag,
  170.                                GL_FALSE,  /* alpha_flag */
  171.                                GL_FALSE,  /* db_flag */
  172.                                GL_FALSE,  /* ximage_flag */
  173.                                0,         /* depth size */
  174.                                0,         /* stencil size */
  175.                                0,         /* accum_size */
  176.                                0          /* level */
  177.                               );
  178.    if (!visual) {
  179.       printf("Couldn't create Mesa/X visual!\n");
  180.       exit(1);
  181.    }
  182.  
  183.    /* Create a Mesa rendering context */
  184.    context = XMesaCreateContext( visual,
  185.                                  NULL       /* share_list */
  186.                                );
  187.    if (!context) {
  188.       printf("Couldn't create Mesa/X context!\n");
  189.       exit(1);
  190.    }
  191.  
  192.    buffer = XMesaCreateWindowBuffer( visual, win );
  193.    if (!buffer) {
  194.       printf("Couldn't create Mesa/X buffer!\n");
  195.       exit(1);
  196.    }
  197.  
  198.  
  199.    XMesaMakeCurrent( context, buffer );
  200.  
  201.    /* Ready to render! */
  202. }
  203.  
  204.  
  205.  
  206. static void draw_cube( void )
  207. {
  208.    /* X faces */
  209.    glIndexi( Red );
  210.    glColor3f( 1.0, 0.0, 0.0 );
  211.    glBegin( GL_POLYGON );
  212.    glVertex3f( 1.0, 1.0, 1.0 );
  213.    glVertex3f( 1.0, -1.0, 1.0 );
  214.    glVertex3f( 1.0, -1.0, -1.0 );
  215.    glVertex3f( 1.0, 1.0, -1.0 );
  216.    glEnd();
  217.  
  218.    glBegin( GL_POLYGON );
  219.    glVertex3f( -1.0, 1.0, 1.0 );
  220.    glVertex3f( -1.0, 1.0, -1.0 );
  221.    glVertex3f( -1.0, -1.0, -1.0 );
  222.    glVertex3f( -1.0, -1.0, 1.0 );
  223.    glEnd();
  224.  
  225.    /* Y faces */
  226.    glIndexi( Green );
  227.    glColor3f( 0.0, 1.0, 0.0 );
  228.    glBegin( GL_POLYGON );
  229.    glVertex3f(  1.0, 1.0,  1.0 );
  230.    glVertex3f(  1.0, 1.0, -1.0 );
  231.    glVertex3f( -1.0, 1.0, -1.0 );
  232.    glVertex3f( -1.0, 1.0,  1.0 );
  233.    glEnd();
  234.  
  235.    glBegin( GL_POLYGON );
  236.    glVertex3f(  1.0, -1.0,  1.0 );
  237.    glVertex3f( -1.0, -1.0,  1.0 );
  238.    glVertex3f( -1.0, -1.0, -1.0 );
  239.    glVertex3f(  1.0, -1.0, -1.0 );
  240.    glEnd();
  241.  
  242.    /* Z faces */
  243.    glIndexi( Blue );
  244.    glColor3f( 0.0, 0.0, 1.0 );
  245.    glBegin( GL_POLYGON );
  246.    glVertex3f(  1.0,  1.0,  1.0 );
  247.    glVertex3f( -1.0,  1.0,  1.0 );
  248.    glVertex3f( -1.0, -1.0,  1.0 );
  249.    glVertex3f(  1.0, -1.0,  1.0 );
  250.    glEnd();
  251.  
  252.    glBegin( GL_POLYGON );
  253.    glVertex3f(  1.0, 1.0, -1.0 );
  254.    glVertex3f(  1.0,-1.0, -1.0 );
  255.    glVertex3f( -1.0,-1.0, -1.0 );
  256.    glVertex3f( -1.0, 1.0, -1.0 );
  257.    glEnd();
  258. }
  259.  
  260.  
  261.  
  262.  
  263. static void display_loop( void )
  264. {
  265.    GLfloat xrot, yrot, zrot;
  266.  
  267.    xrot = yrot = zrot = 0.0;
  268.  
  269.    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  270.    glClearIndex( Black );
  271.  
  272.    glMatrixMode( GL_PROJECTION );
  273.    glLoadIdentity();
  274.    glFrustum( -1.0, 1.0,  -1.0, 1.0,  1.0, 10.0 );
  275.    glTranslatef( 0.0, 0.0, -5.0 );
  276.  
  277.    glMatrixMode( GL_MODELVIEW );
  278.    glLoadIdentity();
  279.  
  280.    glCullFace( GL_BACK );
  281.    glEnable( GL_CULL_FACE );
  282.  
  283.    glShadeModel( GL_FLAT );
  284.  
  285.    while (1) {
  286.       glClear( GL_COLOR_BUFFER_BIT );
  287.       glPushMatrix();
  288.       glRotatef( xrot, 1.0, 0.0, 0.0 );
  289.       glRotatef( yrot, 0.0, 1.0, 0.0 );
  290.       glRotatef( zrot, 0.0, 0.0, 1.0 );
  291.  
  292.       draw_cube();
  293.  
  294.       glPopMatrix();
  295.       glFinish();
  296.  
  297.       xrot += 10.0;
  298.       yrot += 7.0;
  299.       zrot -= 3.0;
  300.    }
  301.  
  302. }
  303.  
  304.  
  305.  
  306.  
  307. int main( int argc, char *argv[] )
  308. {
  309.    if (argc<2) {
  310.       printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
  311.       exit(1);
  312.    }
  313.  
  314.    if (strcmp(argv[1],"-ci")==0) {
  315.       make_window( argv[0], 0 );
  316.    }
  317.    else if (strcmp(argv[1],"-rgb")==0) {
  318.       make_window( argv[0], 1 );
  319.    }
  320.    else {
  321.       printf("Bad flag: %s\n", argv[1]);
  322.       exit(1);
  323.    }
  324.  
  325.    display_loop();
  326.    return 0;
  327. }
  328.  
  329.